/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace DaveSexton.DocProject.Sandcastle.TopicManagement
{
	internal sealed class TopicFiltersToolWindow : ToolWindow
	{
		#region Public Properties
		public static readonly Guid ToolWindowGuid = new Guid("{969F61E5-B9CA-488e-AE97-9926F1E12BE9}");
		public static readonly TopicFiltersToolWindow Instance = new TopicFiltersToolWindow();

		public override System.Windows.Forms.Control Control
		{
			get
			{
				return control;
			}
		}

		public override string Caption
		{
			get
			{
				IDocProject currentProject = TopicExplorer.CurrentProject;

				if (currentProject == null)
					return Resources.Text.TopicFiltersToolWindowCaption;
				else
					return Resources.Text.TopicFiltersToolWindowCaption + " - " + currentProject.Name;
			}
		}

		public override System.Drawing.Bitmap Image
		{
			get
			{
				return Resources.Images.TopicManagement_ToolWindowIcon;
			}
		}
		#endregion

		#region Private / Protected
		private readonly TopicFiltersControl control = new TopicFiltersControl();
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="TopicFiltersToolWindow" /> class.
		/// </summary>
		private TopicFiltersToolWindow()
			: base(ToolWindowGuid)
		{
			control.Hierarchy = TopicExplorer.Instance;
		}
		#endregion

		#region Methods
		public void SyncWithTopicExplorer()
		{
			Window.Caption = Caption;

			control.Manager = TopicExplorer.CurrentManager;
		}
		#endregion

		#region Event Handlers
		protected override void OnShow(EventArgs e)
		{
			TopicExplorer.CurrentManagerChanged += TopicExplorer_CurrentManagerChanged;

			SyncWithTopicExplorer();

			base.OnShow(e);
		}

		protected override void OnHide(EventArgs e)
		{
			TopicExplorer.CurrentManagerChanged -= TopicExplorer_CurrentManagerChanged;

			if (control.Manager != null)
				// NOTE: The TOC must be saved as well since the topics.xml file contains the filter information for conceptual topics
				control.Manager.SaveChanges(TopicManagerChanges.Toc | TopicManagerChanges.Filters | TopicManagerChanges.DynamicFilters);

			base.OnHide(e);
		}

		private void TopicExplorer_CurrentManagerChanged(object sender, EventArgs e)
		{
			SyncWithTopicExplorer();
		}
		#endregion
	}
}
